package ltd.hxya.novel.search.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ltd.hxya.novel.common.bean.Result;
import ltd.hxya.novel.common.to.book.BookTo;
import ltd.hxya.novel.common.vo.SearchParam;
import ltd.hxya.novel.search.service.BookSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/search")
public class BookSearchController {

    @Autowired
    private BookSearchService bookSearchService;

    /**
     * 从elasticsearch中根据查询页面传递参数，模糊查询小说
     */
    @PostMapping("/searchBook")
    public Result<Page<BookTo>> searchBook(@RequestBody SearchParam searchParam) throws IOException {
        //查询商品
        Page<BookTo> bookToPage = bookSearchService.searchBook(searchParam);
        return Result.success(bookToPage);
    }

    @GetMapping("/searchBookByKeyWord")
    public Result<Page<BookTo>> searchBookByKeyWord(String keyWord,Integer page) throws IOException {
        SearchParam searchParam = new SearchParam();
        searchParam.setQueryWord(keyWord);
        searchParam.setPage(page);
        Page<BookTo> bookToPage = bookSearchService.searchBook(searchParam);
        return Result.success(bookToPage);
    }
}
